export default class {
    constructor(my, routes) {
        this.my = my;
        this.routes = routes;
    }

    /**
     * 返回右侧菜单项
     * @returns {*}
     */
    menus() {
        if (!this.my) return [];
        const base = this.routes.filter((i) => i.path === '/')[0] || {};
        return this.filterMenus(base.children);
    }

    /**
     * 判断是否可以访问，不可以则返回可以访问的路由
     * @param route
     * @returns {*}
     */
    notAccess(route) {
        for (let i = 0; i < route.matched.length; i++) {
            const matchedRoute = route.matched[i];
            if (matchedRoute.path !== '') {
                const res = this.accessJudgment(matchedRoute);
                if (res === 'notify') {
                    return res;
                }
                if (!res) {
                    return this.menus()[0];
                }
            }
        }

        return false;
    }

    accessJudgment(route) {
        if (!this.my) return false;
        if (!route.meta) return false;
        if (!route.meta.role) return true;

        if (this.my.type === 3) {
            if (route.meta.role !== 'teacher') return false;
        }

        if (this.my.company_id > 0) {
            // 企业视图: 不允许进入管理员页面
            if (route.meta.role === 'admin') return false;
            if (!route.meta.permission_id) return true;
            if (!this.my.permission.includes(route.meta.permission_id)) return 'notify';
            return true;
        }
        if (this.my.company_id === 0 && this.my.id !== 1) {
            if (route.meta.role === 'company') return false;
            if (!route.meta.permission_id) return true;
            if (!this.my.permission.includes(route.meta.permission_id)) return false;
        }

        return route.meta.role !== 'company';
    }

    /**
     * 角色权限判断
     * @param route
     * @returns {boolean}
     */
    roleJudgment(route) {
        if (!this.my) return false;
        if (!route.meta) return false;
        if (!route.meta.role) return true;

        if (this.my.company_id > 0) {
            // 企业视图: 不允许进入管理员页面
            if (route.meta.role === 'admin' || route.meta.role === 'teacher') {
                return false;
            }
            // return true;
            if (!route.meta.permission_id) {
                return true;
            }
            // 对于属于企业页面, 判断是否拥有权限
            return this.my.permission.includes(route.meta.permission_id);
        }
        // 超管权限: 不允许进入企业类型页面; 判断是否拥有权限
        if (this.my.company_id === 0 && this.my.id !== 1 && this.my.type !== 3) {
            if (route.meta.role === 'company') return false;
            if (!route.meta.permission_id) return true;
            return this.my.permission.includes(route.meta.permission_id);
        }

        if (this.my.type === 3 && route.meta.role !== 'teacher') return false;
        if (this.my.type !== 3 && route.meta.role === 'teacher') return false;

        return route.meta.role !== 'company';
    }

    /**
     * 过滤返回可以显示的菜单
     * @param arr
     */
    filterMenus(arr) {
        return arr.filter((route) => {
            if (!route.meta) return false;
            if (!route.meta.title) return false;
            if (route.meta.disabled) return false;

            return this.roleJudgment(route);
        }).map((v) => {
            if (v.children && v.children.length) {
                v.children = this.filterMenus(v.children);
            }
            if (v.children && !v.children.length) {
                delete v.children;
            }
            if (v.component) delete v.component;

            // 设定默认展开
            // if (index <= 2 && v.children && v.children.length) this.defaultOpeneds.push(v.name);
            return v;
        });
    }
}
